//
//  HomeSectionOne.m
//  JiaoX
//
//  Created by yinqy on 15/1/29.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "HomeSectionOne.h"
#import "HomeBanner.h"
#import "HomeFirstPageHeaderView.h"

#define WIDTH_HEIGHT_RATIO 1.2125

#define COUNT 4

@interface HomeSectionOne()
{
    NSMutableArray *_btnArray;
    HomeBannerList *_bannerList;
}

@end
@implementation HomeSectionOne
- (instancetype)initWithHomeBannerList:(HomeBannerList *)bannerList
{
    self = [super init];
    if (self) {
        _bannerList = bannerList;
        [self setBackgroundColor:[UIColor whiteColor]];
        _btnArray = [NSMutableArray array];
        CGFloat btnWith = JXScreenW/COUNT;
        CGFloat btnHeight = btnWith * WIDTH_HEIGHT_RATIO;
        for (int i=0; i<bannerList.list.count; i++) {
            HomeBanner *banner = bannerList.list[i];
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(i*btnWith, 0, btnWith, btnHeight)];
            [btn setTag:i];
            [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:banner.bannerImg] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            [_btnArray addObject:btn];
        }
        [self setSize:CGSizeMake(JXScreenW, btnHeight)];
    }
    return self;
}
-(void)btnClick:(UIButton *)btn{
    HomeBanner *banner = _bannerList.list[btn.tag];
    if ([self.superview isKindOfClass:[HomeFirstPageHeaderView class]]) {
        HomeFirstPageHeaderView *view  =(HomeFirstPageHeaderView *) self.superview;
        if ([view.delegate respondsToSelector:@selector(sectionOneClick:)]) {
            [view.delegate sectionOneClick:banner.url];
        }
    }
}

@end
